package com.thoughtworks;

import java.util.ArrayList;

public class PrimeFactors {
    public int[] generate(int number) {
        ArrayList<Integer> primes = new ArrayList<Integer>();

        for (int i = (int) Math.sqrt(number); i >= 2; i--) {
            if (number % i == 0 && isPrime(i)) {
                primes.add(0, i);
            }
        }

        return convertArrayListToArray(primes);
    }

    private boolean isPrime(int number) {
        for (int i = (int) Math.sqrt(number); i >= 2; i--) {
            if (number % i == 0) {
                return false;
            }
        }

        return true;
    }

    private int[] convertArrayListToArray(ArrayList<Integer> arrayList) {
        int[] array = new int[arrayList.size()];

        for (int i = 0; i < arrayList.size(); i++) {
            array[i] = arrayList.get(i);
        }
        return array;
    }

    public static void main(String[] args) {
        int[] results = (new PrimeFactors()).generate(330);

        for (int result: results) {
            System.out.print(result + ", ");
        }
    }
}
